import React from 'react'
import { NavLink, useNavigate, useRoutes } from 'react-router-dom'
import routes from './routes'
export default function App() {
    // 创建一个navigate函数，用于编程式导航跳转
    const navigate = useNavigate();
    return (
        <div>
            <div className="row">
                <div className="col-xs-offset-2 col-xs-8">
                    <div className="page-header"><h2>React Router Demo</h2></div>
                </div>
            </div>
            <div className="row">
                <div className="col-xs-2 col-xs-offset-2">
                    <div className="list-group">
                        <NavLink className="list-group-item" to='/about'>About</NavLink>
                        <NavLink className="list-group-item" to='/home'>Home</NavLink>
                        <button onClick={()=>{
                            // 编程式导航的跳转，可以加业务逻辑，比如权限验证等
                            if(localStorage.getItem('token')){
                                navigate('/about');
                            }else{
                                alert('您没付费，看不了')
                            }
                        }}>跳转到About</button>
                        <button onClick={()=>{
                            navigate('/home/message',{
                                replace:false // true 替换当前历史记录. false，生成新的历史记录，默认是false
                            });
                        }}>跳转到Home/message</button>

                        <button onClick={()=>{
                            navigate(1)
                        }}>前进</button>
                        <button onClick={()=>{
                            navigate(-1)
                        }}>后退</button>
                    </div>

                </div>
                <div className="col-xs-6">
                    <div className="panel">
                        <div className="panel-body">
                           {useRoutes(routes)}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    )
}
